package fr.enseirb.mycub.activity.component.layer;

import java.util.ArrayList;
import java.util.List;

import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;

import android.database.Cursor;
import android.graphics.drawable.BitmapDrawable;
import android.provider.BaseColumns;
import fr.enseirb.mycub.PrefManager;
import fr.enseirb.mycub.R;
import fr.enseirb.mycub.activity.AbstractPreferencesActivity;
import fr.enseirb.mycub.activity.MainActivity;
import fr.enseirb.mycub.activity.component.LocationServiceComponent;
import fr.enseirb.mycub.activity.component.MapOverlayComponent;
import fr.enseirb.mycub.component.map.OverlayBundleItem;
import fr.enseirb.mycub.component.map.internet.InternetsOverlay;
import fr.enseirb.mycub.db.InternetDBConstants;
import fr.enseirb.mycub.db.InternetDBConstants;
import fr.enseirb.mycub.db.InternetDBHelper;
import fr.enseirb.mycub.service.UpdateService;
import fr.enseirb.mycub.utils.SeekBarPreference;

public class InternetLayer extends Layer{
	
	public InternetLayer(MapOverlayComponent moc){
	this(moc.getActivity(),moc.getLocationServiceComponent(), moc.getMapView(), moc.getMapOverlays());
	}
	
	public InternetLayer(MainActivity activity, LocationServiceComponent locationServiceComponent,
			MapView mMapView, List<Overlay> mMapOverlays){
		super(activity, locationServiceComponent,mMapView, mMapOverlays);
		BitmapDrawable internetPin = (BitmapDrawable) activity.getResources().getDrawable(
				R.drawable.internetpin);
		internetPin.setBounds(0, 0, internetPin.getIntrinsicWidth(), internetPin
				.getIntrinsicHeight());
		
		mStationsOverlay = new InternetsOverlay(activity.getResources(), internetPin,
				mMapView, activity);
		mMapOverlays.add(mStationsOverlay);
	}

	class ImplPopulateOverlays extends PopulateOverlays {

		public ImplPopulateOverlays(Layer layer) {
			super(layer);
		}
		
		@Override
		protected int getOverlayCount() {
			return InternetDBHelper.getInternetCount(mDBAdapter.getDB());
		}

		@Override
		protected ArrayList<Cursor> getOverlayCursor() {
			return InternetDBHelper.getFilteredInternetCursor(
					mDBAdapter.getDB(),
					new String[] { BaseColumns._ID, InternetDBConstants.KEY_OPEN,
						InternetDBConstants.KEY_LATITUDE,
						InternetDBConstants.KEY_LONGITUDE, InternetDBConstants.KEY_FREE},null,null);
		}

		@Override
		protected ArrayList<? extends OverlayBundleItem> getOverlayFromCursor(ArrayList<Cursor> cursor) {
			return mStationsOverlay.getOverlaysFromCursor(cursor,
					locationServiceComponent.isBound() ? locationServiceComponent.getMyLocation() : null,
							PrefManager.mgr.getBoolean(
									AbstractPreferencesActivity.ENABLE_DISTANCE_FILTER,
									false) ? PrefManager.mgr.getInt(
											AbstractPreferencesActivity.DISTANCE_FILTER,
											SeekBarPreference.DEFAULT_DISTANCE) : 0);
		}
	}

	@Override
	public void createPopulateOverlaysTaskAndExecute() {
		mPopulateOverlays = (PopulateOverlays) new ImplPopulateOverlays(this).execute();
	}

	@Override
	public void createUpdateOverlaysTaskAndExecute() {
		//nothing since we don't update the stations
	}

	@Override
	protected String getIntentAdd() {
		return UpdateService.ACTION_ADD_INTERNET;
	}

	@Override
	protected String getIntentDelete() {
		return UpdateService.ACTION_DELETE_INTERNET;
	}
	
	
}
